/*********************************************************************************
 *      Copyright:  (C) 2022 guoyi<675088383@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  conf.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(25/12/21)
 *         Author:  guoyi<675088383@qq.com>
 *      ChangeLog:  1, Release initial version on "25/12/21 08:11:19"
 *                 
 ********************************************************************************/

#include "conf.h"

int client_parser_conf(const char *conf_file, client_ctx_t *ctx)
{

    dictionary      *ini = NULL;

    if( !conf_file || !ctx )
    {
        printf("Invalid input arguments\n");
        return -1;
    }

    ini = iniparser_load(conf_file);
    if( !ini )
    {
        printf("Cannot parse file: %s\n", conf_file);
        return -2;
    }

    ctx->serv_ip = (char *)iniparser_getstring(ini, "client:serv_ip", NULL);
    if( !ctx->serv_ip )
    {
        return -3;
    }

    ctx->serv_port = iniparser_getint(ini, "client:serv_port", 0);
    if( !ctx->serv_port )
    {
        return -4;
    }

    ctx->interval = iniparser_getint(ini, "client:interval", 0);
    if( !ctx->interval )
    {
        return -5;
    }

    ctx->devid = (char *)iniparser_getstring(ini, "client:devid", NULL);
    if( !ctx->devid )
    {
        return -6;
    }

    ctx->logconf = (char *)iniparser_getstring(ini, "client:logconf", NULL);
    if( !ctx->logconf )
    {
        return -7;
    }

    ctx->db_name = (char *)iniparser_getstring(ini, "client:db_name", NULL);
    if( !ctx->db_name )
    {
        return -8;
    }

    return 0;
}

int server_parser_conf(const char *conf_file, server_ctx_t *ctx)
{

    dictionary      *ini = NULL;

    if( !conf_file || !ctx )
    {
        printf("Invalid input arguments\n");
        return -1;
    }

    ini = iniparser_load(conf_file);
    if( !ini )
    {
        printf("Cannot parse file: %s\n", conf_file);
        return -2;
    }

    ctx->serv_port = iniparser_getint(ini, "server:serv_port", 0);
    if( !ctx->serv_port )
    {
        return -3;
    }

    ctx->logconf = (char *)iniparser_getstring(ini, "server:logconf", NULL);
    if( !ctx->logconf )
    {
        return -4;
    }

    ctx->db_name = (char *)iniparser_getstring(ini, "server:db_name", NULL);
    if( !ctx->db_name )
    {
        return -5;
    }

    return 0;
}
